import { ref } from "vue";
import { getListApi as getAllUserApi } from "@/api/managementCenter/departmentPersonnel";
import { getListApi } from "@/api/configCenter/quotationApproval";
export default function InitHook() {
  const loading = ref<boolean>(false);
  const formState = ref<Array<QuotationApproval.Form>>([
    {
      id: "",
      minPrice: undefined,
      maxPrice: undefined,
      quotationApprovalId: []
    }
  ]);
  const userOpts = ref<Array<Base.GeneraDataType>>([]);
  const getAllUserOpts = () => {
    return getAllUserApi({ pageNum: 1, pageSize: 999999, status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        userOpts.value = data.records || [];
      }
    });
  };
  const init = () => {
    getAllUserOpts();
    loading.value = true;
    return getListApi({ pageNum: 1, pageSize: 99999 } as QuotationApproval.SearchForm)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          if (data.records.length) {
            formState.value = data.records.map((t: any) => {
              return {
                ...t,
                quotationApprovalId: t.bizQuotationApprovalUserList.map((t: any) => t.bizUserId) || []
              };
            });
          } else {
            formState.value = [
              {
                id: "",
                minPrice: 0,
                maxPrice: 0,
                quotationApprovalId: []
              }
            ];
          }
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    formState,
    loading,
    userOpts,
    getAllUserOpts,
    init
  };
}
